package cn.dujc.superkid.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.manager.PieChartManagger;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.CupUnitTestLegendAdapter;
import cn.dujc.superkid.adapter.CupUnitTestResultAdapter;
import cn.dujc.superkid.adapter.UnitTestLegendAdapter;
import cn.dujc.superkid.adapter.UnitTestResultAdapter;
import cn.dujc.superkid.bean.ExamineListEntity;
import cn.dujc.superkid.bean.ResultsEntity;
import cn.dujc.superkid.bean.ScoreStatisticsEntity;
import cn.dujc.superkid.bean.UnitResult;
import cn.dujc.superkid.bean.UnitTestResult;

/**
 * Created by Administrator on 2018/10/16 0016.
 */

public class CupUnitTestResultActivity extends BaseActivity implements CupUnitTestResultAdapter.OnItemClickListener {
    List<ResultsEntity> datas;
    List<ScoreStatisticsEntity> datas1;
    List<ExamineListEntity>datas2;
    RecyclerView recyclerView;
    PieChart pieChart;
    CupUnitTestResultAdapter adapter;
    TextView titleView;
    TextView title1;
    String titles;
    int num;
    String first;
    String total;
    private List<PieEntry> entries = new ArrayList<>();
    private List<Integer> colors = new ArrayList<>();
    private RecyclerView legendRecycler;

    @Override
    public int getViewId() {
        return R.layout.activity_unittest_result;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        datas = getIntent().getParcelableArrayListExtra("unitResult");
        datas1 = getIntent().getParcelableArrayListExtra("statistics");
        datas2=getIntent().getParcelableArrayListExtra("testList");
        for (int i = 0; i < datas1.size(); i++) {
            colors.add(Color.parseColor(datas1.get(i).getColor()));
            entries.add(new PieEntry(Float.parseFloat(datas1.get(i).getScore() + ""), datas1.get(i).getGroup_name()));
        }

        titles = getIntent().getStringExtra("title");
        num = getIntent().getIntExtra("num", 0);
        first = getIntent().getStringExtra("first_score");
        total = getIntent().getStringExtra("total_score");
        initView();
    }

    private void initView() {
        setTitle("考试结果");
        initPieChart();
        title1 = findViewById(R.id.unittest_result_title1);
        title1.setText(titles);
        titleView = findViewById(R.id.unittest_result_title);
        titleView.setText("本试卷共" + num + "题，共计100分，第一次得分为" + first + "分，本次得分" + total + "分");
        recyclerView = findViewById(R.id.unittest_result_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setNestedScrollingEnabled(false);
        adapter = new CupUnitTestResultAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.setDatas(datas);
        adapter.setOnItemClickListener(this);
        legendRecycler=findViewById(R.id.unittest_result_legend);
        legendRecycler.setNestedScrollingEnabled(false);
        legendRecycler.setLayoutManager(new GridLayoutManager(this,3));
        CupUnitTestLegendAdapter unitTestLegendAdapter=new CupUnitTestLegendAdapter(this);
        unitTestLegendAdapter.setDatas(datas1);
        legendRecycler.setAdapter(unitTestLegendAdapter);


    }

    private void initPieChart() {
        pieChart = findViewById(R.id.unittest_result_piechart);
        PieChartManagger pieChartManagger = new PieChartManagger(pieChart);
        pieChartManagger.showSolidPieChart(entries, colors);

    }

    @Override
    public void onItemClick(int pos) {
        Intent intent =new Intent(this,AnalysisActivity.class);
        intent.putExtra("title",datas2.get(pos).getProblem_title());
        intent.putExtra("content",datas2.get(pos).getCorrect_answer().get(0));
        startActivity(intent);
    }
}
